'use strict';

const BaseController = require('./base');
const { addTheme } = require('../validator/theme');

class ThemeLikeController extends BaseController {

    /**
     * 话题点赞
     */
    async like() {
        const themeId = parseInt(this.ctx.params.id);
        const userId = await this.userId();
        const res = await this.service.themeLike.findByThemeIdUserId(themeId, userId);
        if (res) {
            //取消点赞
            this.service.themeLike.delete(themeId, userId);
        } else {
            //点赞
            this.service.themeLike.add(themeId, userId);
        }
        this.ctx.helper.success();
    }

    /**
     * 话题被赞用户列表
     */
    async user() {
        const themeId = parseInt(this.ctx.params.id);
        const page = this.ctx.query.page ? parseInt(this.ctx.query.page) : 0;
        const count = this.ctx.query.count ? parseInt(this.ctx.query.count) : 15;
        const userId = await this.service.themeLike.getUserId(themeId);
        const users = await this.service.user.getThemeUser(userId, page, count);
        this.ctx.helper.success({beans: users});
    }
}

module.exports = ThemeLikeController;